##############################
##     run bootstrap on     ##
##  cleaned cces and output ##
## matrices of distribution ##
##       of opinion by      ##
##        party/class       ##
##   for white voters only  ##
##############################

rm(list = ls())

# setwd('~/../../Class+Party/')

.libPaths('../rpackages')

library(tidyverse)

args <- commandArgs(TRUE)
print(args)

sim <- as.numeric(args[1])

source('bootstrap_funs.R')

options(dplyr.summarise.inform = FALSE)

cces_allyears <- readRDS('cces_allyears.rds') %>% 
  filter(year != 2010)

cces_allyears <- cces_allyears %>% 
  subset_unique_questions()
cces_allyears <- cces_allyears %>%   # drops leaners and weird cross-party identifiers from being d or r
  mutate(pid3 = case_when(pid7 %in% c('Strong Democrat', 'Not very strong Democrat') ~ 'Democrat',
                          pid7 %in% c('Strong Republican', 'Not very strong Republican') ~ 'Republican',
                          pid7 %in% c('Lean Democrat', 'Lean Republican') ~ 'Independent',
                          TRUE ~ pid3))

issuetopics <- cces_allyears %>% 
  select(question, year, pap_topic, topic_3, topic_6) %>% 
  distinct() %>% 
  arrange(question)

parties <- c('dem', 'rep', 'all')

ptm <- proc.time()

output <- list()

for (s in c(sim*2, (sim*2)+1)) {
  set.seed(s)
  
  boot_cces <- boot_sample_cces(cces_allyears, quantile = 0.1)
  
  boot_cces$pid_new <- NA
  boot_cces$pid_new[boot_cces$pid3 %in% c('Republican', 'Democrat')] <- boot_cces$pid3[boot_cces$pid3 %in% c('Republican', 'Democrat')]
  boot_cces$pid_new[boot_cces$pid3 %in% c('Independent', 'Other')] <- 'Independent/Other'
  boot_cces <- boot_cces[,names(boot_cces) != 'pid3']
  
  boot_cces <- boot_cces %>% 
    drop_na(opinion)
  
  
  # class analysis
  
  boot_cces$faminc_group <- NA
  boot_cces$faminc_group[boot_cces$faminc_bottom == 1] <- 'bottom'
  boot_cces$faminc_group[boot_cces$faminc_top == 1] <- 'top'
  boot_cces$faminc_group[boot_cces$faminc_middle == 1] <- 'middle'
  
  
  group_opinions <- boot_cces %>%
    filter(pid_new != 'Independent/Other',
           race_new == 'White') %>% 
    drop_na(pid_new, faminc_group) %>% 
    group_by(pid_new, faminc_group, question) %>% 
    summarize(opinion = sum(opinion) / n()) %>% 
    ungroup() %>% 
    mutate(party_income = paste(pid_new, faminc_group, sep = '_')) %>% 
    select(-pid_new, -faminc_group) %>% 
    spread(key = party_income, value = opinion) %>% 
    arrange(question) %>% 
    column_to_rownames('question')
  
  for (p in c('dem', 'rep')) {
    pname <- case_when(p == 'dem' ~ 'Democrat',
                       p == 'rep' ~ 'Republican')
    
    output[[paste(p, 'inctop', '10', 'sim', s, sep = '_')]] <-  group_opinions[,paste(pname, 'top', sep = '_')]
    output[[paste(p, 'incmiddle', '10', 'sim', s, sep = '_')]] <-  group_opinions[,paste(pname, 'middle', sep = '_')]
    output[[paste(p, 'incbottom', '10', 'sim', s, sep = '_')]] <-  group_opinions[,paste(pname, 'bottom', sep = '_')]
    
  }

}

print((proc.time() - ptm)/60)

saveRDS(output,
        paste0('bootstrap_out/whites/whites_boot_', sim, '.RDS'))

